package com.lagou.java;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @Classname UserClientHandler
 * @Description TODO
 * @Date 2020/7/22 13:20
 * @Created by pc
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext context;
    private String result;
    private String para;
    private RpcRequest data;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        context = ctx;
    }

    // 收到数据, 唤醒线程
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        result = msg.toString();
        notify();
    }

    // 写出数据, 开始等待唤醒
    @Override
    public synchronized Object call() throws Exception {
//        context.writeAndFlush(para);
        System.out.println("--->>> send message :" + data.toString());
        context.writeAndFlush(data);
        wait();
        return result;
    }

    public void setPara(String para) {
        this.para = para;
    }

    public void setData(RpcRequest data) {
        this.data = data;
    }
}
